#include "animeinfodialog.h"
#include "ui_animeinfodialog.h"

#include "flowcontroler.h"
#include "choosevoicedialog.h"
#include "choosetagsdialog.h"
#include <QMessageBox>

AnimeInfoDialog::AnimeInfoDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AnimeInfoDialog)
{
    ui->setupUi(this);

    connect(FlowControler::Interface, &XInterface::addAnimeFinished,this, [=](int ret){
        if(ret == NoError)
        {
            accept();
        }
        else
        {
            QMessageBox::warning(this, "警告", GetErrorMessage(ret));
        }
    });
    connect(FlowControler::Interface, &XInterface::updateAnimeFinished,this, [=](int ret){
        if(ret == NoError)
        {
            accept();
        }
        else
        {
            QMessageBox::warning(this, "警告", GetErrorMessage(ret));
        }
    });

    QStringList casts;
    casts << "声优1"
         << "声优2"
         << "声优3"
         << "声优4"
         << "声优5"
         << "声优6"
         << "声优7"
         << "声优8"
         << "声优9";

    QStringList defaulst = QStringList() << casts[0]<< casts[1];
    ui->editVoice->setText(defaulst.join(","));

    QStringList tags;
    tags << "原创"
         << "漫画改"
         << "小说改"
         << "游戏改"
         << "特摄"
         << "热血"
         << "穿越"
         << "奇幻"
         << "战斗"
         << "搞笑"
         << "日常"
         << "科幻"
         << "治愈"
         << "校园"
         << "泡面"
         << "恋爱"
         << "少女"
         << "魔法"
         << "冒险"
         << "历史"
         << "架空"
         << "运动"
         << "励志"
         << "音乐"
         << "推理"
         << "社团"
         << "智斗"
         << "催泪"
         << "美食"
         << "偶像"
         << "乙女"
         << "职场";

    QStringList defaulst22 = QStringList() << tags[0]<< tags[1];
    ui->editTag->setText(defaulst22.join(","));
//    ui->comboTag->addItems(tags);
    ui->dateEdit->setMinimumDate(QDate(2014,1,1));
    ui->dateEdit->setMaximumDate(QDate::currentDate());
}

AnimeInfoDialog::~AnimeInfoDialog()
{
    delete ui;
}

void AnimeInfoDialog::setAnime(Core::Anime &anime)
{
    isEdit = true;
    m_editAnime = anime;

    ui->editTitle->setText(anime.title);
    ui->dateEdit->setDate(QDate::fromString(anime.releaseDate, DATE_FORMAT));
    ui->comboTeam->setCurrentText(anime.staff);
    ui->editLink->setText(anime.watchLink);
    ui->textEdit->setText(anime.synopsis);
    ui->editVoice->setText(anime.voiceCast.join(","));
    ui->editTag->setText(anime.tags.join(","));
}

void AnimeInfoDialog::on_btnOk_clicked()
{
    Core::Anime anime;
    anime.id = m_editAnime.id;
    anime.title = ui->editTitle->text();
    anime.releaseDate = ui->dateEdit->date().toString(DATE_FORMAT);
    anime.staff = ui->comboTeam->currentText();
    anime.watchLink = ui->editLink->text();
    anime.voiceCast = ui->editVoice->text().split(",");
    anime.synopsis = ui->textEdit->toPlainText();
    anime.tags = ui->editTag->text().split(",");

    if(isEdit)
    {
        FlowControler::Interface->updateAnime(anime);
    }
    else
    {
        FlowControler::Interface->addAnime(anime);
    }
}

void AnimeInfoDialog::on_btnCancel_clicked()
{
    reject();
}

void AnimeInfoDialog::on_btnEditVoice_clicked()
{
    ChooseVoiceDialog dialog;
    dialog.setCasts(ui->editVoice->text().split(","));
    if(ChooseVoiceDialog::Accepted == dialog.exec())
    {
        ui->editVoice->setText(dialog.getCasts().join(","));
    }
}

void AnimeInfoDialog::on_btnEditTag_clicked()
{
    ChooseTagsDialog dialog;
    dialog.setTags(ui->editTag->text().split(","));
    if(ChooseTagsDialog::Accepted == dialog.exec())
    {
        ui->editTag->setText(dialog.getTags().join(","));
    }
}
